﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using AxWMPLib;

namespace MyMp3Player
{
    public partial class Form1 : DevComponents.DotNetBar.Office2007Form
    {
        private List<FileInfo> fileInfos=new List<FileInfo>();
        private int index;
        private AxWindowsMediaPlayer windowsMedia = new AxWindowsMediaPlayer();
        private bool isPlaying = false;
        Timer timer=new Timer();
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            DirectoryInfo directory;
            OpenFileDialog openFile=new OpenFileDialog();
            openFile.Filter = "音乐文件 (.mp3)|*.mp3|All Files (*.*)|*.*";
            //openFile.Multiselect = true;
            if (openFile.ShowDialog()==DialogResult.OK)
            {
                string strPath = openFile.FileName;
                FileInfo fileInfo = new FileInfo(strPath);
                this.listBox1.Items.Add(fileInfo.Name);
                this.fileInfos.Add(fileInfo);
                GetSong(fileInfo);
            }

        }

        private void GetSong(FileInfo fileInfo)
        {
            windowsMedia.currentPlaylist.insertItem(windowsMedia.currentPlaylist.count,
                windowsMedia.newMedia(fileInfo.FullName));
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            if (!isPlaying)
            {
                Player();
            }
        
        }

        private void Player()
        {
            index = this.listBox1.SelectedIndex;
            if (index != -1)
            {
                //windowsMedia.currentPlaylist.insertItem(windowsMedia.currentPlaylist.count,
                //    windowsMedia.newMedia(fileInfos[index].FullName));
                windowsMedia.Ctlcontrols.play();
                timer.Interval = 30;
                timer.Tick += timer_Tick;
                timer.Start();
                isPlaying = true;
            }

            else
            {
                MessageBox.Show("请选一首歌曲！");
            }
        }

        private void timer_Tick(object sender, EventArgs e)
       {
      

           this.progressBarX1.Maximum = (int) windowsMedia.currentMedia.duration;
           this.progressBarX1.Step = (int) (this.progressBarX1.Maximum/20);
            this.progressBarX1.Value =(int) windowsMedia.Ctlcontrols.currentPosition;
            this.labelX1.Text = windowsMedia.currentMedia.durationString;
            this.labelX2.Text = windowsMedia.Ctlcontrols.currentPositionString;
            //progressBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;
            //progressBar1.Minimum = 0;
            //progressBar1.Step = (int)(progressBar1.Maximum / 20);
            //progressBar1.Value = (int)axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
            //lab_xx_xx.Text = axWindowsMediaPlayer1.currentMedia.durationString;
            //lab_now_position.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPositionString;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            this.Controls.Add(windowsMedia);
            windowsMedia.Visible = false;
            
            // axWindowsMediaPlayer.currentPlaylist.insertItem(axWindowsMediaPlayer.currentPlaylist.count, axWindowsMediaPlayer.newMedia(@"C:\Users\付威\Desktop\Let Me Go.mp3"));
            //axWindowsMediaPlayer.Ctlcontrols.play();
            
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog=new FolderBrowserDialog();
            if (folderBrowserDialog.ShowDialog()==DialogResult.OK)
            {
                findAll(folderBrowserDialog.SelectedPath);
            }
        }

        /// <summary>
        /// 递归遍历文件夹
        /// </summary>
        /// <param name="Path">文件夹路径</param>
        public void findAll(string Path)
        {
          

            DirectoryInfo FatherPath = new DirectoryInfo(Path);

            DirectoryInfo[] ChildPath = FatherPath.GetDirectories();

            foreach (FileInfo NextFile in FatherPath.GetFiles())
            {
                if (NextFile.Extension == ".mp3" || NextFile.Extension == ".wma") 
                {
                    listBox1.Items.Add(NextFile.Name);
                    GetSong(NextFile);
                    this.fileInfos.Add(NextFile);
                }
            }


            if (ChildPath.Length != 0)
            {
                for (int i = 0; i < ChildPath.Length; i++)
                {
                    findAll(ChildPath[i].FullName);
                }
            }



        }

       
            //if (isPlaying)
            //{
            //    windowsMedia.Ctlcontrols.stop();
            //}
            //Player();
          
      

        private void buttonX5_Click(object sender, EventArgs e)
        {
            windowsMedia.Ctlcontrols.stop();
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (isPlaying)
            {
                index = this.listBox1.SelectedIndex;
                windowsMedia.Ctlcontrols.stop();
                isPlaying=false;
                windowsMedia.Ctlcontrols.playItem(windowsMedia.currentPlaylist.get_Item(index));
            }
            else
            {
                Player();
            }
         
            
        }

      
    }
}
